/*
 * @文件描述:
 * @版本:
 * @作者: 周晨阳
 * @Date: 2021-01-14 08:26:53
 */
#ifndef TFT_H
#define TFT_H

#include "typedef.h"
#include "public.h"

#ifdef GUI_LVGL
#include "lvgl/lvgl.h"
#include "lvgl/examples/lv_examples.h"
#include "lv_examples/lv_demo.h"

#define DOUBLE_BUFFER 0
#define LVGL_DMA_EN 0
#if GRAM_POSITION == OUTSIDE_CHIP
#define LVGL_BUFFER_SIZE (MONITOR_HOR_RES * 400)
#else
#define LVGL_BUFFER_SIZE (MONITOR_HOR_RES * 60)
#endif

void hal_init(void);
#endif
#ifndef WIN_SIM
#include "stm32f4xx.h"

#endif // !WIN_SIM


/******************************* 定义 ILI934 常用命令 ********************************/
#define CMD_SetCoordinateX 0x2A //设置X坐标
#define CMD_SetCoordinateY 0x2B //设置Y坐标
#define CMD_SetPixel 0x2C       //填充像素

/***************************** ILI934 显示区域的起始坐标和总行列数 ***************************/
#define TFT_DispWindow_X_Star 0 //起始点的X坐标
#define TFT_DispWindow_Y_Star 0 //起始点的Y坐标

#define TFT_LESS_PIXEL (480) //液晶屏较短方向的像素宽度
#define TFT_MORE_PIXEL (854) //液晶屏较长方向的像素宽度

/* 选择BANK1-BORSRAM1 连接 TFT，地址范围为0X60000000~0X63FFFFFF
 * FSMC_A16 接LCD_INFO的DC(寄存器/数据选择)脚
 * 16 bit => FSMC[24:0]对应HADDR[25:1]
 * 寄存器基地址 = 0X60000000
 * RAM基地址 = 0X60020000 = 0X60000000+2^16*2 = 0X60000000 + 0X20000 =
 * 0X60020000 当选择不同的地址线时，地址要重新计算。
 */
//LCD_INFO地址结构体
typedef struct
{
    volatile uint16_t LCD_REG;
    volatile uint16_t LCD_RAM;
} LCD_TypeDef;
//使用NOR/SRAM的 Bank1.sector4,地址位HADDR[27,26]=11        	A6作为数据命令区分线((u32)(0x6C000000 | 0x0000007E))
//使用NOR/SRAM的 Bank1.sector4,地址位HADDR[27,26]=11       	A10作为数据命令区分线((u32)(0x6C000000 | 0x000007FE))
//使用NOR/SRAM的 Bank1.sector1,地址位HADDR[27,26]=00 			A16作为数据命令区分线((u32)(0x60000000 | 0x0001FFFE))
//注意设置时STM32内部会右移一位对其!
#define LCD_BASE ((u32)(0x60000000 | 0x0001FFFE))
#define LCD_INFO ((LCD_TypeDef *)LCD_BASE)

#define LCD_DMA_Stream DMA2_Stream3
#define LCD_DMA_Channel DMA_Channel_0
#define LCD_DMA_IT_TCIFx DMA_IT_TCIF3

// Lcd初始化及其低级控制函数

void Lcd_Initialize(void);
void BlockWrite(unsigned int Xstart, unsigned int Xend, unsigned int Ystart,
                unsigned int Yend);
void LCD_FSMC_Init(void);
void LCD_GPIO_Config(void);
void TFT_GramScan(uint8_t ucOption);
void TFT_Clear(uint16_t usX, uint16_t usY, uint16_t usWidth, uint16_t usHeight);
void TFT_DrawRectangle(uint16_t usX_Start, uint16_t usY_Start, uint16_t usWidth, uint16_t usHeight, uint8_t ucFilled);
void TFT_DrawLine(uint16_t usX1, uint16_t usY1, uint16_t usX2, uint16_t usY2);
void TFT_OpenWindow(uint16_t usX, uint16_t usY, uint16_t usWidth, uint16_t usHeight);
void TFT_BackLed_Control(int State);
void TFT_SetPointPixel(uint16_t usX, uint16_t usY);
uint16_t TFT_GetPointPixel(uint16_t usX, uint16_t usY);
void TFT_ClearFull(uint16_t color);
void TFT_SetBackColor(uint16_t Color);
void TFT_SetTextColor(uint16_t Color);
void TFT_Clear2(unsigned int x0, unsigned int y0, unsigned int x1,
                unsigned int y1, uint16_t color);
void TFT_DrawBitmap(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t *pic);
void TFT_DrawBitmap2(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t *pic, uint8_t direction);
#ifdef WIN_SIM
int get_MeasureX_FromSDL(void);
int get_MeasureY_FromSDL(void);
void TFT_SetPixel(uint16_t usX, uint16_t usY, uint16_t color);
void SDL_Update();
int getSDL_FPS();
void clearSDL_FPS();
void SDL_Draw_H_Line(int x0, int y, int x1, uint16_t color);
#endif
#define White 0xFFFF
#define Black 0x0000
//#define Blue 0x001F
#define Blue2 0x051F
//#define Red 0xF800
#define Magenta 0xF81F
//#define Green 0x07E0
#define Cyan 0x7FFF
#define Yellow 0xFFE0
#define LCD_WIDTH 480
#define LCD_HEIGHT 854
/******************************* TFT ÏÔÊ¾ÆÁµÄ FSMC ²ÎÊý¶¨Òå
 * ***************************/
// FSMC_Bank1_NORSRAM
#define FSMC_Addr_TFT_CMD ((uint32_t)0x60000000)

// FSMC_Bank1_NORSRAM
#define FSMC_Addr_TFT_DATA ((uint32_t)0x60020000)

#define FSMC_Bank1_NORSRAMx FSMC_Bank1_NORSRAM1

//片选信号 NE1
#define TFT_CS_CLK RCC_AHB1Periph_GPIOD
#define TFT_CS_PORT GPIOD
#define TFT_CS_PIN GPIO_Pin_7

//
// tft dc引脚-> FSMC_A16
#define TFT_DC_CLK RCC_AHB1Periph_GPIOD
#define TFT_DC_PORT GPIOD
#define TFT_DC_PIN GPIO_Pin_11

// tft wr引脚
#define TFT_WR_CLK RCC_AHB1Periph_GPIOD
#define TFT_WR_PORT GPIOD
#define TFT_WR_PIN GPIO_Pin_5

// tft rd引脚
#define TFT_RD_CLK RCC_AHB1Periph_GPIOD
#define TFT_RD_PORT GPIOD
#define TFT_RD_PIN GPIO_Pin_4

// tft rst引脚
#define TFT_RST_CLK RCC_AHB1Periph_GPIOF
#define TFT_RST_PORT GPIOF
#define TFT_RST_PIN GPIO_Pin_6

// tft背光引脚
#define TFT_BK_CLK RCC_AHB1Periph_GPIOE
#define TFT_BK_PORT GPIOE
#define TFT_BK_PIN GPIO_Pin_3

/********Êý¾ÝÐÅºÅÏß***************/
#define TFT_D0_CLK RCC_AHB1Periph_GPIOD
#define TFT_D0_PORT GPIOD
#define TFT_D0_PIN GPIO_Pin_14

#define TFT_D1_CLK RCC_AHB1Periph_GPIOD
#define TFT_D1_PORT GPIOD
#define TFT_D1_PIN GPIO_Pin_15

#define TFT_D2_CLK RCC_AHB1Periph_GPIOD
#define TFT_D2_PORT GPIOD
#define TFT_D2_PIN GPIO_Pin_0

#define TFT_D3_CLK RCC_AHB1Periph_GPIOD
#define TFT_D3_PORT GPIOD
#define TFT_D3_PIN GPIO_Pin_1

#define TFT_D4_CLK RCC_AHB1Periph_GPIOE
#define TFT_D4_PORT GPIOE
#define TFT_D4_PIN GPIO_Pin_7

#define TFT_D5_CLK RCC_AHB1Periph_GPIOE
#define TFT_D5_PORT GPIOE
#define TFT_D5_PIN GPIO_Pin_8

#define TFT_D6_CLK RCC_AHB1Periph_GPIOE
#define TFT_D6_PORT GPIOE
#define TFT_D6_PIN GPIO_Pin_9

#define TFT_D7_CLK RCC_AHB1Periph_GPIOE
#define TFT_D7_PORT GPIOE
#define TFT_D7_PIN GPIO_Pin_10

#define TFT_D8_CLK RCC_AHB1Periph_GPIOE
#define TFT_D8_PORT GPIOE
#define TFT_D8_PIN GPIO_Pin_11

#define TFT_D9_CLK RCC_AHB1Periph_GPIOE
#define TFT_D9_PORT GPIOE
#define TFT_D9_PIN GPIO_Pin_12

#define TFT_D10_CLK RCC_AHB1Periph_GPIOE
#define TFT_D10_PORT GPIOE
#define TFT_D10_PIN GPIO_Pin_13

#define TFT_D11_CLK RCC_AHB1Periph_GPIOE
#define TFT_D11_PORT GPIOE
#define TFT_D11_PIN GPIO_Pin_14

#define TFT_D12_CLK RCC_AHB1Periph_GPIOE
#define TFT_D12_PORT GPIOE
#define TFT_D12_PIN GPIO_Pin_15

#define TFT_D13_CLK RCC_AHB1Periph_GPIOD
#define TFT_D13_PORT GPIOD
#define TFT_D13_PIN GPIO_Pin_8

#define TFT_D14_CLK RCC_AHB1Periph_GPIOD
#define TFT_D14_PORT GPIOD
#define TFT_D14_PIN GPIO_Pin_9

#define TFT_D15_CLK RCC_AHB1Periph_GPIOD
#define TFT_D15_PORT GPIOD
#define TFT_D15_PIN GPIO_Pin_10

/******************************* 定义 ILI934 显示屏常用颜色 ********************************/
#define BACKGROUND BLACK //默认背景颜色

#define WHITE 0xFFFF   //白色
#define BLACK 0x0000   //黑色
#define GREY 0xF7DE    //灰色
#define BLUE 0x001F    //蓝色
#define BLUE2 0x051F   //浅蓝色
#define RED 0xF800     //红色
#define MAGENTA 0xF81F //红紫色，洋红色
#define GREEN 0x07E0   //绿色
#define CYAN 0x7FFF    //蓝绿色，青色
#define YELLOW 0xFFE0  //黄色
#define BRED 0xF81F
#define GRED 0xFFE0
#define GBLUE 0x07FF


#endif
